package st.business.tepth.business.visitor.busnumber;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import io.reactivex.disposables.Disposable;
import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.BusNumberBean;
import st.business.tepth.config.Url;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;
import st.shadow.library.core.util.utilcode.util.DeviceUtils;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-03-05 17:24
 */

public class BusNumberModel implements BusNumberContract.Model {

    @Override
    public Disposable getBusList(int id,
                                 final OnDataConvertListener<List<BusNumberBean>> listener) {
        return RxRestClient.builder()
                .url(Url.GetBusNumberList)
                .params("factoryAreaId", id)
                .params("workbenchNo", DeviceUtils.getAndroidID())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(convertToVisitorLocation(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    private List<BusNumberBean> convertToVisitorLocation(String response) {
        return new ObjConverter<List<BusNumberBean>>().convert(response
                , new TypeReference<List<BusNumberBean>>() {
                });
    }
}
